package com.rightsidetech.trade.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.utils.DateUtil;
import com.rightsidetech.trade.dtos.request.TdOrderSalesVolumeDayStatisReq;
import com.rightsidetech.trade.dtos.response.TdCouponSumAmountStatis;
import com.rightsidetech.trade.dtos.response.TdOrderIncomeRefundDayStatisResp;
import com.rightsidetech.trade.dtos.response.TdOrderSalesVolumeDayStatisResp;
import com.rightsidetech.trade.dtos.response.TdStatisIncomeRefundResp;
import com.rightsidetech.trade.entity.TdOrderIncomeSalesDayStatis;
import com.rightsidetech.trade.mapper.TdCommodityOrderDetailsMapper;
import com.rightsidetech.trade.mapper.TdCommodityUserOrderMapper;
import com.rightsidetech.trade.mapper.TdCouponUseRecordMapper;
import com.rightsidetech.trade.mapper.TdOrderIncomeSalesDayStatisMapper;
import com.rightsidetech.trade.service.TdStatisService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service("tdStatisService")
public class TdStatisServiceImpl implements TdStatisService {

    @Resource
    private TdOrderIncomeSalesDayStatisMapper tdOrderIncomeSalesDayStatisMapper;
    @Resource
    private TdCommodityOrderDetailsMapper tdCommodityOrderDetailsMapper;
    @Resource
    private TdCommodityUserOrderMapper tdCommodityUserOrderMapper;
    @Resource
    private TdCouponUseRecordMapper tdCouponUseRecordMapper;

    @Override
    public ResponseData<PageInfo<TdOrderSalesVolumeDayStatisResp>> orderSalesVolumeDayStatisPage(TdOrderSalesVolumeDayStatisReq req) {
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<TdOrderSalesVolumeDayStatisResp> list = tdOrderIncomeSalesDayStatisMapper.selectSalesVolumeList(req);
        return ResponseData.success(new PageInfo<>(list));
    }

    @Override
    public List<TdOrderSalesVolumeDayStatisResp> orderSalesVolumeDayStatisList(TdOrderSalesVolumeDayStatisReq req) {
        return tdOrderIncomeSalesDayStatisMapper.selectSalesVolumeList(req);
    }

    @Override
    public ResponseData<PageInfo<TdOrderIncomeRefundDayStatisResp>> orderIncomeRefundDayStatisPage(TdOrderSalesVolumeDayStatisReq req) {
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<TdOrderIncomeRefundDayStatisResp> list = tdOrderIncomeSalesDayStatisMapper.selectIncomeRefundList(req);
        if(!StringUtils.isEmpty(list) && list.size() > 0){
            for(TdOrderIncomeRefundDayStatisResp resp:list){
                BigDecimal wxIncomeAmount = tdOrderIncomeSalesDayStatisMapper.selectSumIncomeAmountByPayTypeAndStatisAt((byte) 1,resp.getStatisAt());
                BigDecimal otherIncomeAmount = tdOrderIncomeSalesDayStatisMapper.selectSumIncomeAmountByPayTypeAndStatisAt((byte) 2,resp.getStatisAt());
                BigDecimal wxRefundAmount = tdOrderIncomeSalesDayStatisMapper.selectSumRefundAmountByPayTypeAndStatisAt((byte) 1,resp.getStatisAt());
                BigDecimal otherRefundAmount = tdOrderIncomeSalesDayStatisMapper.selectSumRefundAmountByPayTypeAndStatisAt((byte) 2,resp.getStatisAt());
                resp.setWxIncomeAmount(wxIncomeAmount);
                resp.setWxRefundAmount(wxRefundAmount);
                resp.setOtherIncomeAmount(otherIncomeAmount);
                resp.setOtherRefundAmount(otherRefundAmount);
            }
        }
        return ResponseData.success(new PageInfo<>(list));
    }

    @Override
    public List<TdOrderIncomeRefundDayStatisResp> orderIncomeRefundDayStatisList(TdOrderSalesVolumeDayStatisReq req) {
        return tdOrderIncomeSalesDayStatisMapper.selectIncomeRefundList(req);
    }

    @Override
    public ResponseData statisOrderSalesVolumeDay(String statisTime, TdOrderIncomeSalesDayStatis tdStatis3) {
        if(StringUtils.isEmpty(tdStatis3)){
            return ResponseData.fail();
        }

        for (int i = 0; i <= 1; i++) {
            String source = String.valueOf(i);
            //统计订单数量, 销量
            Long tdStatisSalesVolumnCommodityNum = tdCommodityOrderDetailsMapper.selectStatisSalesVolumeCommodityNum(statisTime, tdStatis3.getStoreInfoId(), source);
            Long tdStatisRefundCommodityNum = tdCommodityOrderDetailsMapper.selectStatisRefundCommodityNum(statisTime, tdStatis3.getStoreInfoId(), source);

            Long orderCount = tdCommodityUserOrderMapper.selectStatisSalesVolumeOrderCount(statisTime, tdStatis3.getStoreInfoId(), source);
            Long refundOrderCount = tdCommodityUserOrderMapper.selectStatisRefundOrderCount(statisTime, tdStatis3.getStoreInfoId(), source);

            //判断是否都为0
            boolean flag = tdStatisSalesVolumnCommodityNum != 0 || tdStatisRefundCommodityNum != 0 || orderCount != 0 || refundOrderCount != 0;

            if(flag){
                TdOrderIncomeSalesDayStatis tdStatis = tdOrderIncomeSalesDayStatisMapper.selectByStoreIdAndDate(tdStatis3.getStoreInfoId(), statisTime, source);
                if(StringUtils.isEmpty(tdStatis)){
                    tdStatis3.setOrderCount(orderCount);
                    tdStatis3.setSalesVolume(tdStatisSalesVolumnCommodityNum);
                    tdStatis3.setRefundOrderCount(refundOrderCount);
                    tdStatis3.setRefundSalesVolume(tdStatisRefundCommodityNum);
                    tdStatis3.setCreatedAt(new Date());
                    tdStatis3.setSource((byte) i);
                    tdStatis3.setStatisAt(DateUtil.getDate(statisTime, "yyyy-MM-dd"));
                    if(source.equals("0")){
                        tdStatis3.setPayType((byte) 1);
                    }else{
                        tdStatis3.setPayType((byte) 2);
                    }
                    tdOrderIncomeSalesDayStatisMapper.insertSelective(tdStatis3);
                }else{
                    TdOrderIncomeSalesDayStatis tdStatis2 = new TdOrderIncomeSalesDayStatis();
                    tdStatis2.setId(tdStatis.getId());
                    tdStatis2.setUpdatedAt(new Date());
                    tdStatis2.setOrderCount(orderCount);
                    tdStatis2.setSalesVolume(tdStatisSalesVolumnCommodityNum);
                    tdStatis2.setRefundOrderCount(refundOrderCount);
                    tdStatis2.setRefundSalesVolume(tdStatisRefundCommodityNum);
                    tdOrderIncomeSalesDayStatisMapper.updateByPrimaryKeySelective(tdStatis2);
                }
            }

        }

        return ResponseData.success();
    }

    @Override
    public List<Long> statisOrderSalesVolumeDayByStatis(String statisTime) {
        return tdCommodityUserOrderMapper.selectStoreIdsByStatisTime(statisTime);
    }

    @Override
    public ResponseData statisIncomeRefundDay(String statisTime, TdOrderIncomeSalesDayStatis tdStatis3) {
        if(StringUtils.isEmpty(tdStatis3)){
            return ResponseData.fail();
        }

        //统计订单财务

        for (int i = 0; i <= 1; i++) {
            String source = String.valueOf(i);
            //统计订单数量, 销量
            TdStatisIncomeRefundResp income = tdCommodityUserOrderMapper.selectOnlyIncome(statisTime, tdStatis3.getStoreInfoId(), source);
            TdStatisIncomeRefundResp refund = tdCommodityUserOrderMapper.selectIncomeRefund(statisTime, tdStatis3.getStoreInfoId(), source, (byte) 5);

            //判断是否都为0
            boolean flag = income.getAmount().compareTo(new BigDecimal("0")) != 0 || refund.getAmount().compareTo(new BigDecimal("0")) != 0;

            if(flag){
                TdOrderIncomeSalesDayStatis tdStatis = tdOrderIncomeSalesDayStatisMapper.selectByStoreIdAndDate(tdStatis3.getStoreInfoId(), statisTime, source);
                if(StringUtils.isEmpty(tdStatis)){
                    tdStatis3.setIncomeAmount(income.getAmount());
                    tdStatis3.setRefundAmount(refund.getAmount());
//                tdStatis3.setRefundOrderCount(refund.getOrderCount());
                    tdStatis3.setCreatedAt(new Date());
                    tdStatis3.setSource((byte) i);
                    tdStatis3.setStatisAt(DateUtil.getDate(statisTime, "yyyy-MM-dd"));
                    if(source.equals("0")){
                        tdStatis3.setPayType((byte) 1);
                    }else{
                        tdStatis3.setPayType((byte) 2);
                    }
                    tdOrderIncomeSalesDayStatisMapper.insertSelective(tdStatis3);
                }else{
                    TdOrderIncomeSalesDayStatis tdStatis2 = new TdOrderIncomeSalesDayStatis();
                    tdStatis2.setId(tdStatis.getId());
                    tdStatis2.setUpdatedAt(new Date());
                    tdStatis2.setIncomeAmount(income.getAmount());
                    tdStatis2.setRefundAmount(refund.getAmount());
//                tdStatis2.setRefundOrderCount(refund.getOrderCount());
                    tdOrderIncomeSalesDayStatisMapper.updateByPrimaryKeySelective(tdStatis2);
                }
            }


        }

        return ResponseData.success();
    }

    @Override
    public TdCouponSumAmountStatis statisTotalAmountByActivityId(Long activityId) {
        TdCouponSumAmountStatis statis = tdCouponUseRecordMapper.selectSumByActivityId(activityId);
        return statis;
    }


}
